import axios from "axios";
import qs from 'qs'
let http = axios.create({
    //根路径
    baseURL:"http://localhost:8092/",
    //超时时间
    timeout:1000 * 300,
    //携带凭证
    withCredentials:true,
    //headers:{'X-Requested-With': 'XMLHttpRequest'},

    //发送请求之前转换请求数据
    // transformRequest:function (data) {
    //   console.log(data);//请求参数
    //   return data;
    // }
})

//拦截请求，处理请求内容
http.interceptors.request.use(request=>{
    if (request.method === "post"){
        //格式化参数
        request.data = qs.stringify(request.data);
        request.headers['content-Type'] = 'application/x-www-form-urlencoded';
    }
    let token = sessionStorage.getItem("token");
    if (token){
        var token1 = "bearer "+token;
        request.headers.Authorization = token1;
    }
    return request;
})

//拦截响应，处理请求内容
http.interceptors.response.use(response=>{
    // console.log(response.data.code);
    return response;
})

export default http
